display.function.nm <- function (unit.id, time.id, treatment, data, color.of.treated = "darkblue", 
    color.of.untreated = "lightgrey", title = "Treatment Distribution \n Across Units and Time", 
    xlab = "Time", ylab = "Unit", x.size = NULL, y.size = NULL, 
    legend.position = "none", x.angle = NULL, y.angle = NULL, 
    legend.labels = c("not treated", "treated 1", "treated 2", "treated 3"), decreasing = FALSE, 
    matched.set = NULL, show.set.only = FALSE, hide.x.tick.label = FALSE, 
    hide.y.tick.label = FALSE, gradient.weights = FALSE, dense.plot = FALSE) 
{
    alphaweight <- NULL
    if (!inherits(data, "data.frame")) 
        stop("please convert data to data.frame class")
    if (any(is.na(data[, unit.id]))) 
        stop("Cannot have NA unit ids")
    if (!inherits(data[, time.id], "integer")) 
        stop("please convert time id to consecutive integers")
    if (gradient.weights && is.null(matched.set)) 
        stop("gradient.weights cannot be TRUE without a provided matched set")
    if (!gradient.weights && !show.set.only && !is.null(matched.set)) {
        warning("gradient.weights, show.set.only set to FALSE, but matched set provided. Ignoring matched set")
        matched.set <- NULL
    }
    data <- na.omit(data[c(unit.id, time.id, treatment)])
    colnames(data) <- c("unit.id", "time.id", "treatment")
    data$trintens <- as.numeric(tapply(data$treatment, data$unit.id, 
        mean, na.rm = TRUE)[as.character(data$unit.id)])
    data <- data[order(data$trintens, decreasing = decreasing), 
        ]
    data$unit.id <- factor(data$unit.id, levels = unique(as.character(data$unit.id)))
    data$time.id <- factor(x = data$time.id, levels = sort(unique(data$time.id)), 
        ordered = TRUE)
    if (!is.null(matched.set)) {
        t.id <- as.numeric(unlist(strsplit(names(matched.set), 
            split = "[.]"))[c(T, F)])
        t.t <- as.numeric(unlist(strsplit(names(matched.set), 
            split = "[.]"))[c(F, T)])
        lag <- attr(matched.set, "lag")
        t.range <- (t.t - lag):t.t
        control.ids <- unlist(matched.set)
        wts <- attr(matched.set[[1]], "weights")
        wtd.ids <- as.numeric(names(wts[wts > 0]))
    }
    if (show.set.only) {
        data <- data[data[, "unit.id"] %in% c(t.id, wtd.ids), 
            ]
    }
    if (!gradient.weights) {
        if (!dense.plot) {
            p <- ggplot(data, aes(y = unit.id, x = time.id)) + 
                geom_tile(aes(fill = treatment), colour = "white")
        }
        else {
            p <- ggplot(data, aes(y = unit.id, x = time.id)) + 
                geom_raster(aes(fill = treatment), hjust = 0, 
                  vjust = 0.5)
        }
    }
    if (is.null(matched.set)) {
        clrs <- NULL
        p <- p + scale_fill_gradient(low = color.of.untreated, 
            high = color.of.treated, guide = "legend", breaks = c(0, 
                1,2,3), labels = legend.labels) + theme_bw() + labs(title = title, 
            x = xlab, y = ylab, fill = "") + theme(axis.ticks.x = element_blank(), 
            panel.grid.major = element_blank(), panel.border = element_blank(), 
            legend.position = legend.position, panel.background = element_blank(), 
            axis.text.x = element_text(angle = x.angle, size = x.size, 
                vjust = 0.5), axis.text.y = element_text(size = y.size, 
                angle = y.angle), plot.title = element_text(hjust = 0.5))
    }
    if (!is.null(matched.set) & length(matched.set) == 1 & inherits(matched.set, 
        "matched.set")) {
        if (gradient.weights) {
            wts <- attr(matched.set[[1]], "weights")
            max.wt <- max(wts)
            min.wt <- min(wts[wts > 0])
            low.wt <- min.wt * 0.5
            wts.z <- wts[wts > 0]
            wt <- wts.z[match(data$unit.id, names(wts.z))]
            wt[is.na(wt) | wt == 0] <- low.wt
            data$alphaweight <- wt
            data[!(data$time %in% t.range & data$unit.id %in% 
                wtd.ids), "alphaweight"] <- low.wt
            data[data$unit.id == t.id & data$time %in% t.range, 
                "alphaweight"] <- max.wt
            data$alphaweight <- (data$alphaweight - min(data$alphaweight))/(max(data$alphaweight) - 
                min(data$alphaweight))
            data$alphaweight[data$alphaweight == min(data$alphaweight)] <- min(data$alphaweight[data$alphaweight > 
                0]) * 0.5
        }
        else {
            low.wt <- 1 * 0.5
            max.wt <- 1
            data$alphaweight <- low.wt
            wtd.controls <- names(wts[wts > 0])
            data[data$unit.id %in% c(t.id, wtd.controls), "alphaweight"] <- max.wt
            data[!data$time %in% t.range, "alphaweight"] <- low.wt
        }
        if (!dense.plot) {
            p <- ggplot(data, aes(y = unit.id, x = time.id)) + 
                geom_tile(aes(fill = treatment, alpha = alphaweight), 
                  colour = "white")
        }
        else {
            p <- ggplot(data, aes(y = unit.id, x = time.id)) + 
                geom_raster(aes(fill = treatment, alpha = alphaweight), 
                  hjust = 0, vjust = 0.5)
        }
        p <- p + scale_fill_gradient(low = color.of.untreated, 
            high = color.of.treated, guide = "legend", breaks = c(0, 
                1,2,3), labels = legend.labels) + theme_bw() + labs(title = title, 
            x = xlab, y = ylab, fill = "") + theme(axis.ticks.x = element_blank(), 
            panel.grid.major = element_blank(), panel.border = element_blank(), 
            legend.position = legend.position, panel.background = element_blank(), 
            axis.text.x = element_text(angle = x.angle, size = x.size, 
                vjust = 0.5), axis.text.y = element_text(size = y.size, 
                angle = y.angle), plot.title = element_text(hjust = 0.5))
    }
    if (hide.x.tick.label) {
        p <- p + theme(axis.text.x = element_blank(), axis.ticks.x = element_blank())
    }
    if (hide.y.tick.label) {
        p <- p + theme(axis.text.y = element_blank(), axis.ticks.y = element_blank())
    }
    if (hide.x.tick.label || hide.y.tick.label) {
        pjp <- p
    }
    else {
        pjp <- p + scale_y_discrete(expand = c(0, 0), labels = unique(as.character(data$unit.id))) + 
            ggtitle(title) + xlab(xlab) + ylab(ylab)
    }
    return(pjp)
}
